<template>
  <el-dialog
    custom-class="upd-driver-center"
    v-el-drag-dialog="{ drag: true, full: false, resize: false }"
    title="更新-驾驶员证件资料"
    :visible="true"
    :close-on-click-modal="false"
    @close="closeMd"
    width="600px">
    <CardItem 
      ref="cardItemRef" 
      v-for="it in updateSource"
      :datas="obj"
      :key="it.value"
      :source="it">
    </CardItem>

    <div style="padding: 0 0 20px 20px">
      <el-button 
        icon="el-icon-edit" 
        type="primary" 
        @click="onSubmit" 
        :loading="fm.sb.load">
        {{fm.sb.text}}
      </el-button>
      
      <el-button 
        icon="el-icon-close" 
        @click="closeMd">
        关闭
      </el-button>
    </div>

  </el-dialog>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import CardItem from './card-item';

import * as personManSer from "@/api/services/personMan.service";

export default {
  name: "UpdateDriver",
  directives: {elDragDialog},
  props: {myData: Object},
  components: {CardItem},
  data() {
    return {
      fm: {
        fname: "updDriverCenterFm",
        sb: {load: false, text: '更新'},
        ps: {
          // 驾驶员id
          driverId: '',
          // 证件信息
          remindInfo: '',
        },
      },

      // 修改对象
      obj: null,

      // 项目类型-选择数据
      updateSource: [
        {label: '再教育年审', value: 'RESTUDY_INSPECT'},
        {label: '驾驶证年审', value: 'DRIVER_INSPECT'},
        {label: '资格证年审', value: 'CERTI_INSPECT'},
        {label: '体检有效期', value: 'BODYTEST_INSPECT'},
        {label: '身份证有效期', value: 'IDCARD_EXPIRE'},
        {label: '驾驶证有效期', value: 'DRIVER_EXPIRE'},
      ],

    };
  },
  created() {
    if (this.myData) {
      this.obj = this.myData.obj.reminds;
    }
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", {state: false});
    },
    // 提交表单
    async onSubmit() {
      let that = this, U = that.U, fm = that.fm, sb = fm.sb, ps = U.scopy(fm.ps);

      let fg = true, remindInfo = [];
      if(fg){
        // 获取-更新项对象数组
        let updateArr = this.$refs.cardItemRef;
        
        let validateArr = updateArr.map(it => it.validate()).filter(it => it);
        for (let i = 0; i < validateArr.length; i++) {
          let it = await validateArr[i];
          if(it.validate) remindInfo.push(it.data);
        }
        if(remindInfo.length == 0) fg = U.qerror('至少完成一项填写');
      }
      
      if(fg){
        let params = {
          driverId: this.myData.obj.id,
          remindInfo
        }
        sb.load = true;
        personManSer.updateDriverRemind(params).then(res => {
          sb.load = false;
          
          if (res?.code !== 1) {
            U.qerror(res.msg);
          } else {
            U.qsuccess(res.msg);
            
            that.closeMd();
            that.$emit("reloadDg");
          }
        });

        setTimeout(() => { sb.load = false; }, 10000);
      }
    },


  },
};
</script>
